package org.association.service.impl;

import org.association.link.LinkUtil;
import org.association.service.TradeService;
import org.association.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@Service
public class TradeServiceImpl implements TradeService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public Integer getMemberAsset(String address){
        Map<String, Object> map = LinkUtil.getMemberAsset(restTemplate, address);
        if(Boolean.parseBoolean(map.get("result").toString())){
            return Integer.parseInt(map.get("asset").toString());
        }
        else{
            return -1;
        }
    }


    @Override
    public Integer injectAsset(String address, Integer amount){
        return LinkUtil.injectAsset(restTemplate, address, amount);
    }

    @Override
    public Integer transfer(String fromAddress, String toAddress, Integer amount) {
        return LinkUtil.transfer(restTemplate, fromAddress, toAddress, amount);
    }

    @Override
    public Integer getMemberAssetWBA(String address) {
        Map<String, Object> map = LinkUtil.getMemberAssetWBA(restTemplate, address);
        if(Boolean.parseBoolean(map.get("result").toString())){
            return Integer.parseInt(map.get("asset").toString());
        }
        else{
            return -1;
        }
    }

    @Override
    public Boolean injectAssetWBA(String address, Integer amount) {
        return LinkUtil.injectAssetWBA(restTemplate, address, amount);
    }

    @Override
    public Boolean withdrawAssetWBA(String address, Integer amount) {
        return LinkUtil.withdrawAssetWBA(restTemplate, address, amount);
    }

    @Override
    public Boolean transferWBA(String fromAddress, String toAddress, Integer amount) {
        return LinkUtil.transferWBA(restTemplate, fromAddress, toAddress, amount);
    }

    @Override
    public Boolean batchTransferWBA(List<String> fromAddress, String toAddress, Integer amount) {
        System.out.println(fromAddress.size());
        for(int i = 0; i < fromAddress.size(); i++){
            System.out.println(fromAddress.get(i));
            transferWBA(fromAddress.get(i), toAddress, amount);
        }
        return true;
    }

}
